<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Coupon extends Model
{
    use SoftDeletes;

    protected $table = 'coupon';


    public static function boot()
    {
        parent::boot();

        static::saving(function($model){
            if(isset($model->attributes['service_type'])) {
                unset($model->attributes['service_type']);
            }
        });
    }


    public function service_type()
    {
        return $this->hasManyThrough(
            ServiceType::class,                 // 远程表
            CouponServiceType::class,          // 中间表
            'coupon_id',                    // 中间表对主表的关联字段
            'id',                       // 远程表对中间表的关联字段
            'id',                         // 主表对中间表的关联字段
            'service_type_id'            // 中间表对远程表的关联字段
        );
    }
    public static function selectOption()
    {
        return self::query()->orderBy('id')->pluck('name', 'id');
    }

}
